#include "stdafx.h"
#include "CursadeNodeDef.h"
#include "InstanceTouch.h"
#include "CursadeSearchRule.h"
#include "CursadeReliveRule.h"
#include <algorithm>
#include "LadderBuff.h"
DECL_INSTANCE_TOUCH(ETouch_CursadeNodeDef);

struct  CursadeNodeDefDefReflectionRegister
{
	CursadeNodeDefDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iSearchRule,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CursadeSearchRule,"Rule");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iReliveRule,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CursadeReliveRule,"Rule");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iLadderBuff,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_LadderBuff,"buff");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iRewardDropID,0).Editor_SetAsRaw("DropBox");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iWinAddCoin,0).Editor_SetAsRaw("Bonus");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iwinAddExp,0).Editor_SetAsRaw("Bonus");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iLoseAddCoin,0).Editor_SetAsRaw("Bonus");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iLoseAddExp,0).Editor_SetAsRaw("Bonus");
		REGISTER_REFLECTION_MEMBER(CursadeNodeDef,m_iAddScore,10).Editor_SetAsRaw("TreasureBox");
		
		GlobalClassManager::Instance().RegisterClass(CursadeNodeDef::s_GetReflection(),eDefaultDef_CursadeNodeDef);
		GlobalDefManager::Instance().RegisterDefManger(&CursadeNodeDefManager::Instance());

	}
}___CursadeNodeDefDefReflectionRegister;

rewrite  void CursadeNodeDef::OnArchieveReaded()
{
	m_pSearchRule=CursadeSearchRuleManager::Instance().LookupItem(m_iSearchRule);
	m_pReliveRule=CursadeReliveRuleManager::Instance().LookupItem(m_iReliveRule);
	m_pLadderBuff=LadderBuffManager::Instance().LookupItem(m_iLadderBuff);
	m_pNextNode=NULL;
}
static bool __CompareByNodeID(const CursadeNodeDef *pA,const CursadeNodeDef *pB)
{
	return pA->m_iID<pB->m_iID;
}
void CursadeNodeDefManager::OnArchieveReaded()
{
	__CursadeNodeDefManager::OnArchieveReaded();

	std::vector< CursadeNodeDef *> vItem;
	this->EnumerateEditable(vItem);
	std::sort(vItem.begin(),vItem.end(),__CompareByNodeID);
	for(int32 ii=0;ii<vItem.size();ii++)
	{
		if(ii==vItem.size()-1)
		{
			vItem[ii]->m_pNextNode=NULL;
		}
		else
		{
			vItem[ii]->m_pNextNode=vItem[ii+1];
		}
	}
	if(vItem.size()>0)
	{
		m_pFirstNode= *vItem.begin();
		m_pMaxNode= *vItem.rbegin();
	}
	else
	{
		m_pFirstNode=m_pMaxNode= &DefaultStaticItem();
	}
}